/*
 *  $Id: MessageScrollback.java 4 2008-11-21 20:24:10Z Nathaniel.Waisbrot $
    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.gui.window;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;

import termint.brains.MessageListener;

public class MessageScrollback extends Window implements MessageListener {
	private StyledText buffer;
	private static final int screenMargin = 10;
	private final Rectangle screenSize;
	private final int windowMargins;
	
	private final Color defaultBG, defaultFG;
	private final Color newBG, newFG;
	
	/**
	 * Create a new window holding messages.  
	 * @param display the Display to draw on
	 * @param vt the VT320 backing the display
	 * @param otherWindow the parent window
	 * @param windowMargins space between the parent window
	 */
	public MessageScrollback(Window parent, int windowMargins) {
		super(parent, SWT.MODELESS);
		Display display = parent.getDisplay();
		this.screenSize = display.getClientArea();
		this.windowMargins = windowMargins;
		
		defaultBG = display.getSystemColor(SWT.COLOR_WHITE);
		defaultFG = display.getSystemColor(SWT.COLOR_BLACK);
		newBG = display.getSystemColor(SWT.COLOR_WHITE);
		newFG = display.getSystemColor(SWT.COLOR_RED);
		
		window.setText("Messages");
		window.setLayout(new FillLayout());
		
		resizeUnderWindow(parent.getBounds());
		
		buffer = new StyledText(window, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);		
	}
	
	/**
	 * Resize this window according to a buddy-window.  This window is just as long, and stretches
	 * from the bottom of the buddy to the bottom of the display.
	 * @param otherWindow
	 */
	public void resizeUnderWindow(Rectangle otherWindow) {
		int topEdge = otherWindow.y + otherWindow.height + windowMargins;
		window.setBounds(screenSize.x, 
				topEdge, 
				Math.min(otherWindow.width, screenSize.width - screenMargin), 
				Math.min(200, screenSize.height - topEdge));
	}
	
	public void resizeUnderWindow(Window otherWindow) {
		resizeUnderWindow(otherWindow.getBounds());
	}
	
	public void setBounds(int x, int y, int width, int height) {
		window.setBounds(x, y, width, height);
	}
	
	public void newText(String text) {
		// Reset the style of the old text
		buffer.setStyleRange(new StyleRange(0, buffer.getText().length(), defaultFG, defaultBG));
		buffer.setForeground(newFG);
		buffer.setBackground(newBG);
		buffer.append(text);
	}
	
	public void dispose() {
		buffer.dispose();
		super.dispose();
	}

	public void clear() {
		buffer.setText("");
	}

	public void addKeyListener(KeyListener l) {
		buffer.addKeyListener(l);
	}
	
	public void addListener(int eventType, Listener l) {
		buffer.addListener(eventType, l);		
	}

	@Override
	public void redraw() {
		buffer.redraw();
	}

	@Override public void newQuestion(String text) {}
}
